package org.example.rmsmsapi.Utils;


import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HZY on 2018/1/14.
 */
public class DateUtils {

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    public static String getCurDataString() {
        return dateFormat.format(new Date());
    }

    /**
     * 比较当前日期和输入日期相差的天数
     * @param date
     * @throws ParseException
     */
    public static int  daysBetween( String date) {
        Date date2=new Date();
        Date date3=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date2=sdf.parse(sdf.format(date2));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            date3=    sdf.parse(sdf.format(sdf.parse(date)));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar cal =Calendar.getInstance();
        cal.setTime(date2);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date3);
        long time2 = cal.getTimeInMillis();
        long between_days=(time1-time2)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**获取当前时间格式如2017-08-12 08 23:59.212**/
    public static  String  Getdnowdatexx(){
        Date date1=new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    /**
     * 获取时间格式如20180407
     * @return
     */
    public static String getStringToday(){
        Date currentTimeDate=new Date();
        SimpleDateFormat formater =new SimpleDateFormat("yyyMMdd");
        String dString=formater.format(currentTimeDate);
        return dString;
    }

    /**
     * 通过身份证号获取年龄
     * @param idCard
     * @return
     */
    public static int getAgeByIdCard(String idCard) {
        int iAge = 0;
        Calendar cal = Calendar.getInstance();
        String year = idCard.substring(6, 10);
        int iCurrYear = cal.get(Calendar.YEAR);
        iAge = iCurrYear - Integer.valueOf(year);
        return iAge;
    }

    /**转换时间格式如2017-08-12 08 00**/
    public static  String  converttimetoYYMMDDHH00(String date){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        try {
            date1=sdf.parse(date);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm.ss");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    public static  String  converttimetoYYMMDD(String date){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        try {
            date1=sdf.parse(date);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    /**
     * 判断输入时间是否等于当日
     * @param ghrq
     * @return
     */
    public static boolean  datesdiferrent(String ghrq) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        Date date1 = null;
        Date date =new Date();
        try {
            date1=sdf.parse(ghrq);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        String Bigdadate=sdf.format(date1);
        String nowdateString=sdf.format(date);
        boolean ok=false;
        if(Bigdadate.equals(nowdateString)){
            ok=true;
        }else{ok=false;}
        return ok;
    }
    /** 
  * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数 
  */public static String getNextDay(String nowdate, String delay) { 
 try{ 
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String mdate = ""; 
 Date d = strToDate(nowdate); 
 long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60; 
 d.setTime(myTime * 1000); 
 mdate = format.format(d); 
 return mdate; 
        }catch(Exception e){ 
 return ""; 
 } 
 }
/***获取输入时间的前30分钟时间*/
 public static String GetTimeByParam(String date,int delaytime,int param){
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
     Date date1 = null;
     try {
         date1=sdf.parse(date);

     } catch (ParseException ex) {
         System.out.print("jycx"+"时间转换有错误");
     }
     System.out.print(date1);
     Date date2=null;
     if(param==1){
     date2= new Date(date1.getTime()-delaytime);}
     else {
     date2=    new Date(date1.getTime()+delaytime);
     }
     String reSting=sdf.format(date2);
     return reSting;
 }
 public static  long compareTimeMM(String date1,String date2){
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
     Date begin=null;
     Date end=null;
     try {
         begin=sdf.parse(date1);
         end=sdf.parse(date2);

     } catch (ParseException e) {
         e.printStackTrace();
     }
     long between = (end.getTime() - begin.getTime())/1000;
     long min = between/60;
     return min;
 }
 
 /** 
* 将短时间格式字符串转换为时间 yyyy-MM-dd  
 *  
  * @param strDate 
 * @return 
 */
        public static Date strToDate(String strDate) {  
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       ParsePosition pos = new ParsePosition(0);  
       Date strtodate = formatter.parse(strDate, pos);  
       return strtodate;  
        }



    private static String getFirstDay( String date ) {


        Date theDate=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            theDate = df.parse(df.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
        return str.toString();
    }

    /**
     * 通过身份证号码获取出生日期、性别、年龄
     * @param certificateNo
     * @return 返回的出生日期格式：1990-01-01   性别格式：F-女，M-男
     */
    public static Map<String, String> getBirAgeSex(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8) + "-"
                    + certificateNo.substring(8, 10) + "-"
                    + certificateNo.substring(10, 12);
            sexCode = Integer.parseInt(certificateNo.substring(14,15)) % 2 == 0 ? "2" : "1";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "-"
                    + certificateNo.substring(10, 12) + "-"
                    + certificateNo.substring(12, 14);
            sexCode = Integer.parseInt(certificateNo.substring(16,17)) % 2 == 0 ? "2" : "1";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;
    }



    /**
     * 通过身份证号码获取出生日期、性别、年龄
     *
     * @param certificateNo
     * @return 返回的出生日期格式：1990-01-01   性别格式：F-女，M-男
     */
    public static Map<String, String> getBirAgeSex1(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;
        //判断字符是否为数字
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag) return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        //分别获取年月日 性别  年龄
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8) + "-"
                    + certificateNo.substring(8, 10) + "-"
                    + certificateNo.substring(10, 12);
            sexCode = Integer.parseInt(certificateNo.substring(14,15)) % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "-"
                    + certificateNo.substring(10, 12) + "-"
                    + certificateNo.substring(12, 14);
            sexCode = Integer.parseInt(certificateNo.substring(16,17)) % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;

    }


    /**
     * 根据身份证号判断当前月龄
     * @param cardNo
     * @return
     */
    public static int getAge(String cardNo) {
        String birthday = cardNo.substring(6, 14);
        Date birthdate = null;
        try {
            birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar currentDay = new GregorianCalendar();
        currentDay.setTime(birthdate);
        int birYear = currentDay.get(Calendar.YEAR);

// 获取年龄
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String thisYear = simpleDateFormat.format(new Date());
        int age = Integer.parseInt(thisYear) - birYear;
        return age;
    }

    /**通过身份证号获取年龄的月数如20岁则输出为240
     * **/
    public  static  int getAgeMothByIdCard(String idCard){
        String birth = idCard.substring(6, 14);
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMdd");
        //时间解析
        DateTime birthday = DateTime.parse(birth, format);
        DateTime now = new DateTime();
        int month = Months.monthsBetween(birthday, now).getMonths();
        return month;
    }
    /**获取系统的年份**/
    public static String getCurrentYear(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }
      /**获取系统月份**/
    public static String getSysMoth() {
        Calendar calendar = Calendar.getInstance();
        String Moth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        return Moth;
    }






}



